<?php

namespace App\Store\Models\Member;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\Account;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Order\OrderSalesGoods;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class Member extends Model
{
    protected $table = 'site_member_list';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['lvinfo', 'shoppingaccountinfo', 'storeinfo','sharememinfo'])->where(['cp_id' => $cp_id, 'is_del' => 1])->orderBy('create_at', 'desc');

        $isgetall = intval($request->isgetall) ?? 0;
        $store_id = intval($request->store_id);

        $real_name = $request->real_name;
        $mobile = $request->mobile;
        if(!empty($real_name)|| !empty($mobile)){
            if (!empty($real_name)) {
                $query->where('real_name', 'like', '%' . $real_name . '%');
            }
            if (!empty($mobile)) {
                $query->where('mobile', 'like', '%' . $mobile . '%');
            }
        }else{
            if ($store_id) {
                $query->where('store_id', $store_id);
            }
        }


        $ids=$request->ids;
        $ids=is_array($ids)?$ids:(is_array(json_decode($ids,true))?json_decode($ids,true):[]);
        if(count($ids)>0){
            $query->whereIn('id',$ids);
        }


        $mem_card_no = $request->mem_card_no;
        if ($mem_card_no != '') {
            $query->where('mem_card_no', 'like', '%' . $mem_card_no . '%');
        }

        $shopping_account_id = $request->shopping_account_id;
        if ($shopping_account_id != '') {
            $query->where('shopping_account_id', $shopping_account_id);
        }

        $member_lv = intval($request->member_lv);
        if ($member_lv>0) {
            $query->where('member_lv', $member_lv);
        }
        $is_band_wxaccount = $request->is_band_wxaccount;
        if ($is_band_wxaccount != '') {
            $query->where('is_band_wxaccount', $is_band_wxaccount);
        }

        $lunar_bdate = $request->lunar_bdate;
        if ($lunar_bdate != '') {

            $m = Carbon::createFromFormat('Y-m-d', $lunar_bdate)->month;
            $d = Carbon::createFromFormat('Y-m-d', $lunar_bdate)->day;
            $query->whereMonth('lunar_bdate',$m)->whereDay('lunar_bdate',$d);
        }
        $solar_bdate = $request->solar_bdate;
        if ($solar_bdate != '') {

            $month = Carbon::createFromFormat('Y-m-d', $solar_bdate)->month;
            $day = Carbon::createFromFormat('Y-m-d', $solar_bdate)->day;
            $query->whereMonth('solar_bdate',$month)->whereDay('solar_bdate',$day);

        }

        $points_current_min = intval($request->points_current_min);
        $points_current_max = intval($request->points_current_max);

        if ($points_current_min > 0 && $points_current_max > $points_current_min) {

            $query->where('points_current', '>=', $points_current_min)->where('points_current', '<=', $points_current_max);
        }


        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        $last_shopping_start_time = strtotime($request->last_shopping_start_time) > 0 ? strtotime($request->last_shopping_start_time) : intval($request->last_shopping_start_time);
        $last_shopping_end_time = strtotime($request->last_shopping_end_time) > 0 ? strtotime( sprintf('%s %s', $request->last_shopping_end_time, '23:59:59')) : intval($request->last_shopping_end_time);
        if ($last_shopping_start_time > 0 && $last_shopping_end_time > $last_shopping_start_time) {
            $query->where('last_shopping_at', '>=', $last_shopping_start_time)->where('last_shopping_at', '<=', $last_shopping_end_time);
        }


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();


            $data= Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            $data= ['list' => $list];
        }


        foreach ($data['list'] as &$val){
            $val['pre_money'] =$val['pre_money']+$val['pointstobalance'];
        }

        return $data;
    }


    public function lvinfo()
    {
        return $this->hasOne(SiteMemberLv::class, 'id', 'member_lv')->select(['id','lv_name']);
    }

    public function shoppingaccountinfo()
    {

        return $this->hasOne(Account::class, 'id', 'shopping_account_id')->select(['id','real_name']);
    }

    public function storeinfo()
    {

        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select(['id','store_name']);
    }

    public function sharememinfo(){
        return $this->hasOne(Member::class, 'id', 'share_mem_id')->where('is_del',1)->select(['id','real_name']);
    }

    /**
     * 消费记录
     */
    public static function getShoppingLogList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['lvinfo', 'shoppingaccountinfo', 'storeinfo'])->where(['cp_id' => $cp_id, 'is_del' => 1])->orderBy('create_at', 'desc');


        $store_id = intval($request->store_id);
        if ($store_id) {
            $query->where('store_id', $store_id);
        }


        $real_name = $request->real_name;
        if ($real_name != '') {
            $query->where('real_name', $real_name);
        }
        $mobile = $request->mobile;
        if ($mobile != '') {
            $query->where('mobile', $mobile);
        }

        $mem_card_no = $request->mem_card_no;
        if ($mem_card_no != '') {
            $query->where('mem_card_no', 'like', '%' . $mem_card_no . '%');
        }


        $member_lv = $request->member_lv;
        if ($member_lv != '') {
            $query->where('member_lv', $member_lv);
        }


        $lunar_bdate = $request->lunar_bdate;
        if ($lunar_bdate != '') {
            $query->where('lunar_bdate', $lunar_bdate);
        }
        $points_current_min = intval($request->points_current_min);
        $points_current_max = intval($request->points_current_max);

        if ($points_current_min > 0 && $points_current_max > $points_current_min) {

            $query->where('points_current', '>=', $points_current_min)->where('points_current', '<=', $points_current_max);
        }




        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }
        $shopping_account_id = $request->shopping_account_id;
        if ($shopping_account_id != '') {
            $query->where('shopping_account_id', $shopping_account_id);
        }



        //订单查询条件
        $where=[];
        // $shopping_account_id = $request->shopping_account_id;
        // if ($shopping_account_id>0) {
        //     $where[]=['shopping_account_id', $shopping_account_id];
        // }
        $where[]=['is_del',1];
        $goods_sn_code = $request->goods_sn_code;
        if ($goods_sn_code != '') {
            $where[]=['goods_sn_code', $goods_sn_code];
        }

        $orderstart_time = strtotime($request->orderstart_time) > 0 ? strtotime($request->orderstart_time) : intval($request->orderstart_time);
        $orderend_time = strtotime($request->orderend_time) > 0 ? strtotime(sprintf('%s %s', $request->orderend_time, '23:59:59')) : intval(strtotime(sprintf('%s %s', date("Y-m-d", $request->orderend_time), '23:59:59')));
        if ($orderstart_time > 0 && $orderend_time > $orderstart_time) {
            $where[]=['create_at', '>=', $orderstart_time];
            $where[]=['create_at', '<=', $orderend_time];
        }

        if($request->isdoexport==1){
            $list=$query->get()->toArray();
            $re_orderlist=[];

            foreach($list as $val){
                //订单,获取销售订单
                $wheremem=$where;
                $wheremem[]=['mem_id',$val['id']];
                $order_list=OrderSalesGoods::with(['memberinfo','storeinfo','orderinfo','shoppingaccountinfo1','memberinfo.storeinfo'])->where($wheremem)->get()->toArray();

                foreach($order_list as $val1){
                    $re_orderlist[]=$val1;
                }


            }


            return $re_orderlist;

        }else{
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            $list=Common::paginateDataToArray($list);

            foreach($list['list'] as &$val){
                //订单,获取销售订单
                $wheremem=$where;
                $wheremem[]=['mem_id',$val['id']];
                $val['order_list']=OrderSalesGoods::with(['storeinfo','orderinfo','shoppingaccountinfo1','memberinfo.storeinfo'])->where($wheremem)->get()->toArray();
            }


            return $list;
        }


    }
}
